package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Override
    public IPage<RoomItemVo> getRoomList(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomList(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomById(Long id) {
        //1.查询roomInfo信息
        RoomInfo roomInfo = roomInfoMapper.getRoomInfoByid(id);
        //2.根据房间id获取公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
        //3.根据房间id获取图片信息
        List<GraphVo> graphVoList = graphInfoMapper.getListByRoomId(ItemType.ROOM, id);
        //4.根据房间id获取属性信息
        List<AttrValueVo> attrValueList = attrValueMapper.getListByRoomId(id);
        //5.根据房间id获取配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getListByRoomId(id);
        //6.根据房间id获取标签信息
        List<LabelInfo> labelInfoList = roomInfoMapper.listLabelByRoomId(id);
        //7.根据房间id获取支付方式信息
        List<PaymentType> paymentTypeList = paymentTypeMapper.getByRoomId(id);
        //8.根据房间id获取杂费信息
        List<FeeValueVo> feeValueList = feeValueMapper.getListByRoomId(roomInfo.getApartmentId());
        //9.根据房间id获取租期信息
        List<LeaseTerm> leaseTermList = leaseTermMapper.getListByRoomId(id);
        //10.查看房间是否可以入住
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id);
        lambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        long count = leaseAgreementService.count(lambdaQueryWrapper);
        RoomDetailVo appRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, appRoomDetailVo);
        appRoomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        appRoomDetailVo.setIsCheckIn(count > 0);
        appRoomDetailVo.setApartmentItemVo(apartmentItemVo);
        appRoomDetailVo.setGraphVoList(graphVoList);
        appRoomDetailVo.setAttrValueVoList(attrValueList);
        appRoomDetailVo.setFacilityInfoList(facilityInfoList);
        appRoomDetailVo.setLabelInfoList(labelInfoList);
        appRoomDetailVo.setPaymentTypeList(paymentTypeList);
        appRoomDetailVo.setFeeValueVoList(feeValueList);
        appRoomDetailVo.setLeaseTermList(leaseTermList);

        return appRoomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> getRoomListByApartId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}





